//
//  FriendRequestViewController.m
//  SIPphone
//
//  Created by wrt on 13-11-26.
//  Copyright (c) 2013年 wrtsoft. All rights reserved.
//

#import "FriendRequestViewController.h"
#import "FriendRespondDateBase.h"
#import "FrdAddFalseMsgViewController.h"
#import "FrdAddSucceedViewController.h"
#import "FrdRjctMsgViewController.h"
#import "FrdRqstMsgDetailViewController.h"
#import "FriendRespondMessage.h"


@interface FriendRequestViewController ()
@property (assign, nonatomic) int begin;

@end

@implementation FriendRequestViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // Custom initialization
        self.title = @"好友请求信息";
    }
    return self;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    FriendRespondDateBase *friendRspdDB =[FriendRespondDateBase shareFriendRespondDateBase];
//    [friendRspdDB readDB];
    [self.tableView reloadData];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.begin = 0;
    // Do any additional setup after loading the view from its nib.
    FriendRespondDateBase *friendRspdDB =[FriendRespondDateBase shareFriendRespondDateBase];
    [friendRspdDB readDB];
    [friendRspdDB readDBForpartWithBeginNumber:self.begin];
    
    self.userNameArray = [[NSMutableArray alloc] init];
    self.displayNameArray = [[NSMutableArray alloc] init];
    self.ipPortArray = [[NSMutableArray alloc] init];
    self.isReadArray = [[NSMutableArray alloc] init];
    self.timeArray = [[NSMutableArray alloc] init];
    self.isRqtOrRjtArray = [[NSMutableArray alloc] init];
    self.isDoArray = [[NSMutableArray alloc] init];
    
    [self.userNameArray addObjectsFromArray:friendRspdDB.partUserNameArray];
    [self.displayNameArray addObjectsFromArray:friendRspdDB.partDisplayNameArray];
    [self.ipPortArray addObjectsFromArray:friendRspdDB.partIpPortArray];
    [self.isReadArray addObjectsFromArray:friendRspdDB.partIsReadArray];
    [self.timeArray addObjectsFromArray:friendRspdDB.partTimeArray];
    [self.isRqtOrRjtArray addObjectsFromArray:friendRspdDB.partIsRqtOrRjtArray];
    [self.isDoArray addObjectsFromArray:friendRspdDB.partIsDoArray];
    
    //删除编辑按钮
    UIBarButtonItem *deleteButton = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStyleBordered target:self action:@selector(deleteButtonPressed)];
    self.navigationItem.rightBarButtonItem = deleteButton;
    
    //设置通知来刷新界面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadtableView:) name:@"RELOADTABLEVIEWFRIENDREQUEST" object:nil];
}

//编辑按钮操作
- (void)deleteButtonPressed
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:@"请选择要进行的操作" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"清楚全部记录",@"清楚单个记录", nil];
    [alertView show];
}

#pragma mark -
#pragma mark tableViewMethod
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.timeArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
    }
    
    
    NSString *tempString = [[NSString alloc] init];
    
    if ([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"请求"])
    {
        cell.textLabel.text = @"请求信息";
        tempString = @"发来好友请求信息";
        cell.imageView.image = [UIImage imageNamed:@"xiaoxi_1.png"];
    }
    else if([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"拒绝"])
    {
        cell.textLabel.text = @"拒绝信息";
        tempString = @"拒绝了您的好友请求";
        cell.imageView.image = [UIImage imageNamed:@"xiaoxi_jvjuan.png"];
    }
    else if([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"添加失败"])
    {
        cell.textLabel.text = @"添加失败信息";
        tempString = @"不存在或者当前不在线";
        cell.imageView.image = [UIImage imageNamed:@"xiaoxi_shibai.png"];
    }
    else if([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"同意添加"])
    {
        cell.textLabel.text = @"同意添加信息";
        tempString = @"同意了您的好友请求";
        cell.imageView.image = [UIImage imageNamed:@"xiaoxi_chenggong.png"];
    }
    
    
    cell.textLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    if ([self.isReadArray[indexPath.row] isEqualToString:@"未读"])
    {
        cell.textLabel.textColor = [UIColor blueColor];
        cell.detailTextLabel.textColor = [UIColor blueColor];
    }
    
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@/用户%@ %@",self.timeArray[indexPath.row],self.userNameArray[indexPath.row],tempString];
    
    cell.tag = indexPath.row + 1;
    
    return cell;
}



//cell选择后的处理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FriendRespondDateBase *friendRspdDB =[FriendRespondDateBase shareFriendRespondDateBase];
    //获取选择的cell是哪一个
    UITableViewCell *cell = (UITableViewCell *)[self.view viewWithTag:(indexPath.row + 1)];
    cell.textLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    
    if ([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"请求"])
    {
        if ([self.isReadArray[indexPath.row] isEqualToString:@"未读"])
        {
            [friendRspdDB updateDBForReadWithUserName:self.userNameArray[indexPath.row] andTime:self.timeArray[indexPath.row]];
            [self.isReadArray replaceObjectAtIndex:indexPath.row withObject:@"已读"];
        }
            FrdRqstMsgDetailViewController *frdRqstVC = [[FrdRqstMsgDetailViewController alloc] initWithUserName:self.userNameArray[indexPath.row] andDisplayName:self.displayNameArray[indexPath.row] andipPort:self.ipPortArray[indexPath.row] andIsDo:self.isDoArray[indexPath.row]];
            [self.navigationController pushViewController:frdRqstVC animated:YES];
    }
    else if([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"拒绝"])
    {
        if ([self.isReadArray[indexPath.row] isEqualToString:@"未读"])
        {
            [friendRspdDB updateDBForReadWithUserName:self.userNameArray[indexPath.row] andTime:self.timeArray[indexPath.row]];
            [self.isReadArray replaceObjectAtIndex:indexPath.row withObject:@"已读"];
        }
            FrdRjctMsgViewController *frdRjctMsg = [[FrdRjctMsgViewController alloc] initWithUserName:self.userNameArray[indexPath.row] andDisplayName:self.displayNameArray[indexPath.row] andipPort:self.ipPortArray[indexPath.row]];
            [self.navigationController pushViewController:frdRjctMsg animated:YES];
        
    }
    else if([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"添加失败"])
    {
        if ([self.isReadArray[indexPath.row] isEqualToString:@"未读"])
        {
            [friendRspdDB updateDBForReadWithUserName:self.userNameArray[indexPath.row] andTime:self.timeArray[indexPath.row]];
            [self.isReadArray replaceObjectAtIndex:indexPath.row withObject:@"已读"];
        }
        FrdAddFalseMsgViewController *frdFalseMsg = [[FrdAddFalseMsgViewController alloc] initWithUserName:self.userNameArray[indexPath.row] andDisplayName:self.displayNameArray[indexPath.row] andipPort:self.ipPortArray[indexPath.row]];
        [self.navigationController pushViewController:frdFalseMsg animated:YES];
    }
    else if([self.isRqtOrRjtArray[indexPath.row] isEqualToString:@"同意添加"])
    {
        if ([self.isReadArray[indexPath.row] isEqualToString:@"未读"])
        {
            [friendRspdDB updateDBForReadWithUserName:self.userNameArray[indexPath.row] andTime:self.timeArray[indexPath.row]];
            [self.isReadArray replaceObjectAtIndex:indexPath.row withObject:@"已读"];
        }
        FrdAddSucceedViewController *frdSucceed = [[FrdAddSucceedViewController alloc] initWithUserName:self.userNameArray[indexPath.row] andDisplayName:self.displayNameArray[indexPath.row] andipPort:self.ipPortArray[indexPath.row]];
        [self.navigationController pushViewController:frdSucceed animated:YES];
    }
    
    
}

//编辑模式下可删除的方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //数据库执行删除
    FriendRespondDateBase *friendRspdDB = [FriendRespondDateBase shareFriendRespondDateBase];
    [friendRspdDB deleteFriendBDWithUserName:self.userNameArray[indexPath.row] andTime:self.timeArray[indexPath.row]];
    
    [self.userNameArray removeObjectAtIndex:indexPath.row];
    [self.displayNameArray removeObjectAtIndex:indexPath.row];
    [self.ipPortArray removeObjectAtIndex:indexPath.row];
    [self.isReadArray removeObjectAtIndex:indexPath.row];
    [self.timeArray removeObjectAtIndex:indexPath.row];
    [self.isRqtOrRjtArray removeObjectAtIndex:indexPath.row];
    [self.isDoArray removeObjectAtIndex:indexPath.row];
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    
//    [self.tableView reloadData];
    
}

#pragma mark -
#pragma mark alertViewMethod
//UIActionSheet 对应按钮的操作
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        
    }
    if (buttonIndex == 1)
    {
        //删除全部记录
        FriendRespondDateBase *friendRspdDB = [FriendRespondDateBase shareFriendRespondDateBase];
        [friendRspdDB removeAllFriendRespond];
        [self.userNameArray removeAllObjects];
        [self.displayNameArray removeAllObjects];
        [self.ipPortArray removeAllObjects];
        [self.isReadArray removeAllObjects];
        [self.timeArray removeAllObjects];
        [self.isRqtOrRjtArray removeAllObjects];
        [self.isDoArray removeAllObjects];
        [self.tableView reloadData];
    }
    if (buttonIndex == 2)
    {
        //进入编辑模式
        [self.tableView setEditing:!self.tableView.editing];
    }
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.tableView.contentOffset.y >= self.tableView.contentSize.height - self.tableView.frame.size.height)
    {
        FriendRespondDateBase *friendRspdDB = [FriendRespondDateBase shareFriendRespondDateBase];
        self.begin = self.timeArray.count;
        [friendRspdDB readDBForpartWithBeginNumber:self.begin];
        
        [self.userNameArray addObjectsFromArray:friendRspdDB.partUserNameArray];
        [self.displayNameArray addObjectsFromArray:friendRspdDB.partDisplayNameArray];
        [self.ipPortArray addObjectsFromArray:friendRspdDB.partIpPortArray];
        [self.isReadArray addObjectsFromArray:friendRspdDB.partIsReadArray];
        [self.timeArray addObjectsFromArray:friendRspdDB.partTimeArray];
        [self.isRqtOrRjtArray addObjectsFromArray:friendRspdDB.partIsRqtOrRjtArray];
        [self.isDoArray addObjectsFromArray:friendRspdDB.partIsDoArray];
        
        [self.tableView reloadData];
    }
}

- (void)reloadtableView:(NSNotification *)notification
{
    dispatch_async(dispatch_get_main_queue(),^{
        FriendRespondMessage *friendResMsg = [[FriendRespondMessage alloc] init];
        friendResMsg = (FriendRespondMessage *)notification.object;
        [self.userNameArray insertObject:friendResMsg.userName atIndex:0];
        [self.displayNameArray insertObject:friendResMsg.disPlayName atIndex:0];
        [self.ipPortArray insertObject:friendResMsg.ip atIndex:0];
        [self.isReadArray insertObject:@"未读" atIndex:0];
        [self.timeArray insertObject:friendResMsg.time atIndex:0];
        [self.isDoArray insertObject:@"未处理" atIndex:0];
        
        
        switch (friendResMsg.status)
        {
            case 0:
                [self.isRqtOrRjtArray insertObject:@"添加失败" atIndex:0];
                break;
            case 1:
                [self.isRqtOrRjtArray insertObject:@"同意添加" atIndex:0];
                break;
            case 2:
                [self.isRqtOrRjtArray insertObject:@"同意添加" atIndex:0];
                break;
            case 6:
                [self.isRqtOrRjtArray insertObject:@"同意添加" atIndex:0];
                break;
            case 7:
                [self.isRqtOrRjtArray insertObject:@"同意添加" atIndex:0];
                break;
            case 11:
                [self.isRqtOrRjtArray insertObject:@"拒绝" atIndex:0];
                break;
            case 12:
                [self.isRqtOrRjtArray insertObject:@"请求" atIndex:0];
                break;
            default:
                break;
        }
        
        //刷新表格
        [self.tableView reloadData];
    });
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RELOADTABLEVIEWFRIENDREQUEST" object:nil];
}

@end
